/**
 * 
 * 
 *  
 *====================================================
 * 文件名称: SimpleEncryptor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2010-1-22			
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package com.devframe.sys.encrypt;

/**
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;SimpleEncryptor&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --简单加密解密算法--&lt;/b&gt;
 * </pre>
 * <pre>不支持全角字符，只支持数字、字母和半角字符。</pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 *   SimpleEncryptor obj = new SimpleEncryptor();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class SimpleEncryptor implements Encryptor{
    /**
     * 加密
     * @param value 待加密值
     * @return 加密结果
     * 
     */
    public String encrypt(String value) {
        if ((value == null) || (value.trim() == null)) {
            return "";
        }
        int len = value.length();
        char[] enPwd = new char[len * 2 + 1];

        int[] key = { 52, 112, 48, 76, 64, 114, 49, 36 };
        int i = 0;
        for (int j = 0; i < len; ++j) {
            if (j == 8) {
                j = 0;
            }
            int tmpi = value.charAt(i) ^ key[j];
            String hexStr = Integer.toString(tmpi, 16);
            if (hexStr.length() > 1) {
                enPwd[i * 2] = hexStr.charAt(0);
                enPwd[i * 2 + 1] = hexStr.charAt(1);
            } else {
                enPwd[i * 2] = '0';
                enPwd[i * 2 + 1] = hexStr.charAt(0);
            }
            ++i;
        }
        return new String(enPwd).trim();
    }

    /**
     * 解密
     * @param value 待解密值
     * @return 解决结果
     * 
     */
    public String decrypt(String value) {
        if ((value == null) || (value.trim() == null)) {
            return "";
        }
        int len = value.length();
        if (len % 2 != 0) {
            return "";
        }
        char[] decChars = new char[len / 2];
        char[] hexChars = new char[2];

        int[] key = { 52, 112, 48, 76, 64, 114, 49, 36 };
        int i = 0;
        for (int j = 0; i < len / 2; ++j) {
            if (j == 8) {
                j = 0;
            }

            hexChars[0] = value.charAt(i * 2);
            hexChars[1] = value.charAt(i * 2 + 1);

            int tmpi = Integer.parseInt(String.valueOf(hexChars), 16);

            decChars[i] = (char) (tmpi ^ key[j]);

            ++i;
        }

        return new String(decChars).trim();
    }

    /**
     * testEncDec 测试加密和解密
     * @param value
     *            测试值
     * 
     */
    public void testEncDec(String value) {
        String encResult = encrypt(value);
        String decResult = decrypt(encResult);
        System.out.println(value + "->" + encResult + "->" + decResult);

    }

    /**
     * main
     * @param args
     *            参数
     * 
     */
    public static void main(String[] args) {
        try {
            SimpleEncryptor test = new SimpleEncryptor();
            test.testEncDec("厦门市工程有限公司");
            test.testEncDec("信息资源管理系统");
            test.testEncDec("123456");
            test.testEncDec("（）+-【】\\[]'");
            test.testEncDec("abcde");
            test.testEncDec("ABCDE");
            test.testEncDec("EDCBA");
            test.testEncDec("A    A");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }    
}
